﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Runtime.Serialization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Serialization
    Friend NotInheritable Class DependencyStoreSurrogate
        Implements ISerializationSurrogate
        ' Methods
        Friend Sub New()
        End Sub

        Private Sub System.Runtime.Serialization.ISerializationSurrogate.GetObjectData(ByVal obj As Object, ByVal info As SerializationInfo, ByVal context As StreamingContext) Implements ISerializationSurrogate.GetObjectData
            Dim dictionary As IDictionary([Of] DependencyProperty, Object) = [TryCast](obj,IDictionary([Of] DependencyProperty, Object))
            If (dictionary Is Nothing) Then
                Throw New ArgumentException("obj")
            End If
            Dim list As New ArrayList
            Dim list2 As New ArrayList
            Dim pair As KeyValuePair([Of] DependencyProperty, Object)
            For Each pair In dictionary
                If pair.Key.DefaultMetadata.IsNonSerialized Then
                    [Continue] For
                End If
                If pair.Key.IsKnown Then
                    list.Add(pair.Key.KnownIndex)
                Else
                    list.Add(pair.Key)
                End If
                list2.Add(pair.Value)
            Next
            info.AddValue("keys", list.ToArray)
            info.AddValue("values", list2.ToArray)
            info.SetType(GetType(DependencyStoreRef))
        End Sub

        Private Function System.Runtime.Serialization.ISerializationSurrogate.SetObjectData(ByVal obj As Object, ByVal info As SerializationInfo, ByVal context As StreamingContext, ByVal selector As ISurrogateSelector) As Object Implements ISerializationSurrogate.SetObjectData
            Return Nothing
        End Function


        ' Nested Types
        <Serializable> _
        Private NotInheritable Class DependencyStoreRef
            Implements IObjectReference, IDeserializationCallback
            ' Methods
            Private Sub System.Runtime.Serialization.IDeserializationCallback.OnDeserialization(ByVal sender As Object) Implements IDeserializationCallback.OnDeserialization
                If (Not Me.store Is Nothing) Then
                    Dim i As Integer
                    For i = 0 To Me.keys.Count - 1
                        Dim key As DependencyProperty = [TryCast](Me.keys.Item(i),DependencyProperty)
                        If (key Is Nothing) Then
                            key = DependencyProperty.FromKnown(CByte(Me.keys.Item(i)))
                        End If
                        Me.store.Add(key, Me.values.Item(i))
                    Next i
                End If
                Me.store = Nothing
            End Sub

            Private Function System.Runtime.Serialization.IObjectReference.GetRealObject(ByVal context As StreamingContext) As Object Implements IObjectReference.GetRealObject
                If (Me.store Is Nothing) Then
                    Me.store = New Dictionary([Of] DependencyProperty, Object)
                End If
                Return Me.store
            End Function


            ' Fields
            Private keys As IList
            <NonSerialized> _
            Private store As IDictionary([Of] DependencyProperty, Object)
            Private values As IList
        End Class
    End Class
End Namespace

